package com.system.borrow.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;

import com.system.borrow.Constant.TagData;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.TagAdapter;
import com.system.borrow.bean.Tag;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;
import com.system.borrow.widget.MyGridView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 意见反馈
 */
public class CallBackActivity extends BaseActivity implements View.OnClickListener {
    MyGridView gv_tag;
    TagAdapter adapter;
    private EditText et_data;
    private Button btn_subimt;
    String Tag = "";
    List<Tag> list = new ArrayList<Tag>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_back);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("设置", null);
        initView();
        addAction();
        initListener();
    }

    private void addAction() {
        adapter = new TagAdapter(this);
        adapter.setList(TagData.getTags());
        gv_tag.setAdapter(adapter);
    }

    private void initView() {
        gv_tag = (MyGridView) findViewById(R.id.gv_tag);
        et_data = (EditText) findViewById(R.id.et_data);
        btn_subimt = (Button) findViewById(R.id.btn_subimt);
    }

    private void initListener() {
        btn_subimt.setOnClickListener(this);
        gv_tag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Tag tag = (Tag) adapter.getItem(i);
                list.clear();
                if (tag.isSelect()) {
                    tag.setSelect(false);
                } else {
                    tag.setSelect(true);
                }
                for (int j = 0; j < adapter.getList().size(); j++) {
                    if (adapter.getList().get(j).isSelect()) {
                        list.add(adapter.getList().get(j));
                    }
                }
                if (list.size() > 0) {
                    String tags;
                    for (int z = 0; z < list.size(); z++) {
                        tags = list.get(z).getName();
                        if (z == 0) {
                            Tag = tags;
                        } else {
                            Tag += "/" + tags;
                        }
                    }
                    et_data.setText(Tag + ":");
                } else {
                    et_data.setText("");
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_subimt:
                String copule = et_data.getText().toString();
                if (copule.isEmpty()) {
                    ToastShow("请输入反馈意见");
                } else {
                    copule(copule);
                }
                break;
        }
    }

    /**
     * 反馈意见
     *
     * @param copule
     */
    private void copule(String copule) {
        HttpParams params = new HttpParams();
        params.put("textContent", copule);
        OkHttpUtils.post(URLDefind.COUPLE_BACK)
                .tag(this)
                .cacheMode(CacheMode.NO_CACHE)
                .params(params)
                .execute(new DialogCallback<String>(this, String.class, null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                        try {
                            JSONObject json = new JSONObject(s);
                            int statusCode = json.getInt("statusCode");
                            if (statusCode == 200) {
                                ToastShow("意见反馈成功,我们会尽快处理的。");
                                finish();
                            } else {
                                ToastShow(json.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }
}
